package com.kl.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kl.domain.FreightDTO;
import com.kl.domain.OrderDTO;
import com.kl.em.ConstantEnum;
import com.kl.em.Result;
import com.kl.entity.TabOrder;
import com.kl.mapper.TabOrderMapper;
import com.kl.service.IOrderService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-10-30
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderService iOrderService;
    @Autowired
    private TabOrderMapper orderMapper;

    @ApiOperation(value = "订单表增加接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orders", value = "订单表实体类集合", required = true,
                    paramType = "body", dataTypeClass = List.class)
    })
    @PostMapping("/addOrder")
    public Result<List<Long>> addOrder(@RequestBody List<OrderDTO> orders){
        List<Long> idlist = new ArrayList();
        for (OrderDTO orderDTO : orders) {
            //生成订单

            String id = RandomStringUtils.randomNumeric(16);
            idlist.add(Long.parseLong(id));
            orderDTO.setId(Long.parseLong(id));
            orderDTO.setOrderCreationtime(new Date());
            TabOrder order = new TabOrder();
            //copy实体类
            BeanUtils.copyProperties(orderDTO,order);
            iOrderService.addOrder(order);
        }


        return Result.build(ConstantEnum.SUCCESS).setResult(idlist);
    }


    @ApiOperation(value = "订单表修改接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderDTO", value = "订单表实体类", required = true,
                    paramType = "body", dataTypeClass = TabOrder.class)
    })
    @PutMapping("/updateOrder")
    public Result<OrderDTO> updateOrder(@RequestBody OrderDTO orderDTO){
        TabOrder order = new TabOrder();
        //copy实体类
        BeanUtils.copyProperties(orderDTO,order);
        Result result =iOrderService.updateOrder(order);
        return result;
    }

    @ApiOperation(value = "订单表通过id查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "订单表id", required = true,
                    paramType = "path", dataTypeClass = Long.class)
    })
    @GetMapping("/findOrderById/{id}")
    public Result<OrderDTO> findOrderById(@PathVariable("id") Long id){
        TabOrder order =iOrderService.findOrderById(id);
        OrderDTO orderDTO = new OrderDTO();
        BeanUtils.copyProperties(order,orderDTO);
        return Result.build(ConstantEnum.SUCCESS).setResult(orderDTO);
    }


//    @ApiOperation(value = "订单表查询接口")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "订单表id", required = true,
//                    paramType = "quest", dataTypeClass = Long.class)
//    })
//    @GetMapping("/updateOrderBacke")
//    public Result updateOrderBacke(){
//        Order order = iOrderService.updateOrderBacke();
//        return Result.build(ConstantEnum.SUCCESS).setResult(order);
//    }


    @ApiOperation(value = "订单表分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber", value = "当前页数", required = true,
                    paramType = "path", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true,
                    paramType = "path", dataTypeClass = Integer.class),
    })
    @GetMapping("/{pageNumber},/{pageSize}")
    public Result<List<OrderDTO>> findAll(@PathVariable("pageNumber") Integer pageNumber, @PathVariable("pageSize") Integer pageSize) {
        Page<TabOrder> page = new Page<>(pageNumber, pageSize, false);
        IPage<TabOrder> iPage = iOrderService.page(page);
        List<TabOrder> orders = iPage.getRecords();
        String s = JSONObject.toJSONString(orders);
        List<OrderDTO> orderDTOS = JSONObject.parseArray(s, OrderDTO.class);
        return Result.build(ConstantEnum.SUCCESS).setResult(orderDTOS);
    }


    @ApiOperation(value = "根据支付宝订单号修改订单接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serialNumber", value = "支付宝流水号", required = true,
                    paramType = "query", dataTypeClass = String.class),
            @ApiImplicitParam(name = "alyOrderNumber", value = "支付宝订单号", required = true,
                    paramType = "query", dataTypeClass = String.class)
    })
    @PutMapping("/updateOrderStatus")
    public Result updateOrderAlyOrderNumber(@RequestParam String serialNumber,@RequestParam String alyOrderNumber){
        Result result = iOrderService.updateOrderAlyOrderNumber(serialNumber,alyOrderNumber);
        return result;
    }






    @ApiOperation(value = "订单表通过手机号查询订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "senderPhone", value = "手机号", required = true,
                    paramType = "query")
    })
    @GetMapping("/findOrdertoPhone")
    public Result<List<OrderDTO>> findOrdertoPhone(@RequestParam String senderPhone) {

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.like("sender_phone",senderPhone);
        List<TabOrder> list = orderMapper.selectList(wrapper);
        String s = JSON.toJSONString(list);
        List<OrderDTO> tabOrders = JSONObject.parseArray(s, OrderDTO.class);

        return Result.build(ConstantEnum.SUCCESS).setResult(tabOrders);
    }



}
